#! /bin/bash
#用于视觉测距项目的安装

cd ~

#更新或克隆工程
if [ -d "fscv" ]; then
  echo "pull from remote"
  cd fscv
  git config pull.rebase false
  git pull
else
  echo "clone from remote"
  git clone https://gitee.com/zhtroy/fscv.git
  cd fscv
  #仅当工程第一次clone时设置开机启动
  sudo sed -i '$ i /home/pi/fscv/start.sh' /etc/rc.local
fi

pip install --upgrade pip

#安装virtualenv
if command -v virtualenv >/dev/null 2>&1; then
  echo "virtualenv exists"
else
  pip install virtualenv-20.17.1-py3-none-any.whl || pip install virtualenv 
  if [ $? -eq 0 ]; then
    echo "PATH=\$PATH:/home/pi/.local/bin" >> ~/.bashrc
    source ~/.bashrc 
  else
    exit 1
  fi
fi



#检查fscv-env是否存在
if [ ! -d "fscv-env" ]; then
  virtualenv fscv-env
fi

#进入虚拟环境
source fscv-env/bin/activate

# Get packages required for OpenCV
if pip list | grep opencv-python; then
  echo "opencv exists"
else 
  sudo apt-get -y install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
  sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
  sudo apt-get -y install libxvidcore-dev libx264-dev
  sudo apt-get -y install libatlas-base-dev

  # Need to get an older version of OpenCV because version 4 has errors
  pip3 install opencv-python==3.4.10.37 --verbose
fi

# Get packages required for TensorFlow
# Using the tflite_runtime packages available at https://www.tensorflow.org/lite/guide/python
# Will change to just 'pip3 install tensorflow' once newer versions of TF are added to piwheels

#pip3 install tensorflow

version=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')

if [ $version == "3.7" ]; then
pip3 install https://github.com/google-coral/pycoral/releases/download/release-frogfish/tflite_runtime-2.5.0-cp37-cp37m-linux_armv7l.whl
fi

if [ $version == "3.5" ]; then
pip3 install https://github.com/google-coral/pycoral/releases/download/release-frogfish/tflite_runtime-2.5.0-cp35-cp35m-linux_armv7l.whl
fi

if [ $version == "3.9" ]; then
pip3 install tflite_runtime-2.5.0.post1-cp39-cp39-linux_armv7l.whl || pip3 install https://github.com/google-coral/pycoral/releases/download/v2.0.0/tflite_runtime-2.5.0.post1-cp39-cp39-linux_armv7l.whl
fi

# apriltag 
pip3 install dt-apriltags

# CAN
pip3 install python-can==4.1.0

# usb.rules
sudo cp usb.rules /etc/udev/rules.d/usb.rules
sudo udevadm trigger
